﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    private Door doorExit;
    private PlayerConllter player;
    public bool gameOver;

    public GameObject continueBtn;
    public GameObject newBtn;
    public GameObject quitBtn;

    public float playerHealth = 3;

    public void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        player = FindObjectOfType<PlayerConllter>();
        doorExit = FindObjectOfType<Door>();
        if (PlayerPrefs.HasKey("scence"))
        {
            if (continueBtn)
            {
                continueBtn.SetActive(true);
            }
            if(quitBtn && newBtn){
                quitBtn.transform.position = new Vector3(quitBtn.transform.position.x,quitBtn.transform.position.y-100,0);
                newBtn.transform.position = new Vector3(newBtn.transform.position.x,newBtn.transform.position.y-100,0);
            }
        }
    }

    public void Start()
    {
        
    }

    public void Update()
    {
        gameOver = player.isDead;
        UIManager.instance.GameOverUI(player.isDead);
    }

    //重新开始游戏
    public void RestartScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        PlayerPrefs.DeleteKey("playerHealth");
    }

    public void NextScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        PlayerPrefs.SetInt("scence", SceneManager.GetActiveScene().buildIndex + 1);
        SaveData();
    }

    public void QuitGame()
    {
        Application.Quit();
    }
    public void ContinueGame()
    {
        int scence = PlayerPrefs.GetInt("scence");
        SceneManager.LoadScene(scence);
    }
    public void NewGame()
    {
        SceneManager.LoadScene(1);
        PlayerPrefs.DeleteAll();
    }
    public float LoadHealth(float health = 3)
    {
        if (!PlayerPrefs.HasKey("playerHealth"))
        {
            PlayerPrefs.SetFloat("playerHealth", health);
        }

        float currentHealth = PlayerPrefs.GetFloat("playerHealth");
        return currentHealth;
    }

    public void SaveData()
    {
        PlayerPrefs.SetFloat("playerHealth", player.health);
        PlayerPrefs.Save();
    }
}
